package com.example.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ErrorPageRegistrar registerErrorPages() {
        // 此处通过lambda表达式生成了ErrorPageRegistrar接口的匿名实现类
        return registry -> {
            String index = "/index.html";
            if (this.getClass().getResource("/static" + index) == null) {
                index = "/default.html";
            }
            // 将404的响应重定向到index，避免前端路由刷新后404
            registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, index));
        };
    }
}